package com.my.company.admin.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.my.company.admin.service.AdminService;
import com.my.company.admin.service.ChangePathService;
import com.my.company.admin.service.DeptService;
import com.my.company.admin.service.HonerService;
import com.my.company.user.pojo.Admin;
import com.my.company.user.pojo.ChangePath;
import com.my.company.user.pojo.Dept;
import com.my.company.user.pojo.Honer;
import com.my.company.user.pojo.User;
import com.my.company.user.service.UserService;
import com.my.company.util.MD5;
import com.my.company.util.ValidateUtil;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller("adminAction")
@Scope("prototype")
public class AdminAction extends ActionSupport {
	@Resource
	private AdminService adminService;
	private Admin admin;
	@Resource
	private DeptService deptService;
	@Resource
	private UserService userService;
	private String message;
	private List<Dept> deptList;
	private Dept dept;
	private User user;
	private HttpServletRequest request = ServletActionContext.getRequest();
	private String score;
	private List<User> userList;
	@Resource
	private ChangePathService cps;
	@Resource
	private HonerService hs;
	private String reason;
	private String condition;
	Honer honer = new Honer();
	private String rePwd;
	private Integer offset = 0;
	private Integer total = 0;
	private Integer flag = 4;
	public String addHoner() {
		user.setUserId(user.getUserId());
		return SUCCESS;
	}

	// 得到所有用户分页显示
	public String getByPage() {
		userList = userService.getAll();
		if (flag == 0) {
			offset -= 10;
		} else if (flag == 1) {
			offset += 10;
		} else if (flag == 2) {
			offset = userList.size() - 10;
		} else {
			offset = 0;
		}
		if (userList.size() % 10 > 0) {
			total = userList.size() / 10 + 1;
		} else {
			total = userList.size() / 10;
		}
		String sql = "from User";
		userList = userService.getListForPage(sql, offset, 10);
		return SUCCESS;
	}

	// 查看用户详情并修改
	public String editByUserId() {
		user = userService.getEntity(user.getUserId());
		deptList = deptService.findEntityByHQL("from Dept d where 1=?", 1);
		
		return SUCCESS;
	}

	// 修改用户详情并保存
	public String adminEditUser() {
		User user2 = userService.getEntity(user.getUserId());
		dept = deptService.getEntity(user.getDepartId());
		//用户名
		user2.setUserName(user.getUserName());
		//部门名称
		user2.setDempName(dept.getDeptName());
		//姓名
		user2.setEmpName(user.getEmpName());
		//员工编号
		user2.setMpcode(user.getMpcode());
		//身份证号
		user2.setIdcard(user.getIdcard());
		//性别
		user2.setGender(user.getGender());
		//籍贯
		user2.setUserNative(user.getUserNative());
		//民族
		user2.setNation(user.getNation());
		//备注
		user2.setMemo(user.getMemo());
		if (!(user2.getDepartId()).equals(user.getDepartId())) {
			ChangePath changePath = new ChangePath();
			changePath.setFromDept(user2.getDempName());
			changePath.setToDept(user.getDempName());
			changePath.setUserId(user.getUserId());
			cps.saveEntity(changePath);
			
		}
		userService.updateEntity(user2);
		request.getSession().setAttribute("user", user2);
		return SUCCESS;
	}

	public String honerManage() {
		if ("0".equals(score)) {
			return SUCCESS;
		}
		// 加减号
		String str = score.charAt(0) + "";
		int s = 0;

		if (!"".equals(score)) {
			// 分数
			s = (int) Integer.valueOf(score.substring(1));
		}
		if ("-".equals(str)) {
			honer.setReason(reason);
			honer.setTotal(score);
			honer.setUserId(user.getUserId());
			hs.saveEntity(honer);
			user = userService.getEntity(user.getUserId());
			if (user.getTotalScore() < s) {
				// 用来计算
				user.setTotalScore(0);
				// 用来显示
				user.setTotal("0");
			} else {
				int t = user.getTotalScore() - s;
				user.setTotal(t + "");
				user.setTotalScore(t);
			}
			userService.updateEntity(user);
		} else if ("+".equals(str)) {
			honer.setReason(reason);
			honer.setTotal(score);
			honer.setUserId(user.getUserId());
			hs.saveEntity(honer);
			user = userService.getEntity(user.getUserId());
			// TotalHoner总分数
			user.setTotalHoner(user.getTotalScore() + s);

			int t = user.getTotalScore() + s;
			user.setTotal(t + "");
			user.setTotalScore(t);

			userService.updateEntity(user);
		}
		return SUCCESS;
	}

	public String showHoner() {
		String sql = "from User u where 1=? order by u.totalScore desc";
		userList = userService.findEntityByHQL(sql, 1);

		return SUCCESS;
	}

	// 显示用户详情
	public String userDetail() {
		user = userService.getEntity(user.getUserId());
		return SUCCESS;
	}
	//模糊查询
	public String seachUserByCondition() {
		userList = userService.getAllByCondition(condition);
		return SUCCESS;

	}
	public String showDept() {
		String hql = "from Dept where 1=?";
		deptList = deptService.findEntityByHQL(hql, 1);
		return SUCCESS;
	}

	public String addDept() {
		return SUCCESS;
	}

	public String exitAdmin() {
		request.getSession().setAttribute("admin", "");
		request.getSession().removeAttribute("admin");
		return SUCCESS;
	}

	public String saveDept() {
		String hql1 = "from Dept where 1=?";
		List<Dept> deptList = deptService.findEntityByHQL(hql1, 1);
		Integer max = 0;
		for (Dept d : deptList) {
			if (d.getDeptId() > max) {
				max = d.getDeptId();
			}
		}
		dept.setDeptId(max + 1);
		deptService.saveEntity(dept);
		return SUCCESS;
	}

	public String editDept() {
		dept = deptService.getEntity(dept.getDeptId());
		return SUCCESS;
	}

	public String editDeptSuccess() {

		deptService.updateEntity(dept);
		return SUCCESS;
	}

	public String dropDept() {
		deptService.deleteEntity(dept);
		return SUCCESS;
	}

	public String adminRegist() {
		return SUCCESS;

	}

	public String adminAdd() {
		admin.setPwd(MD5.GetMD5Code(admin.getPwd()));
		adminService.saveEntity(admin);
		return SUCCESS;
	}

	public String adminLogin() {
		if (ValidateUtil.isValid(admin.getEmail())
				&& ValidateUtil.isValid(admin.getPwd())) {
			admin.setPwd(MD5.GetMD5Code(admin.getPwd()));
			String hql = "from Admin where email=? ";
			List<Admin> adminList = adminService.findEntityByHQL(hql,
					admin.getEmail());
			if (adminList.size() == 0) {
				message = "*亲，不存在您的账号";
				return "wrong";
			} else {
				String hql2 = "from Admin where email=? and pwd=?";
				List<Admin> adminList2 = adminService.findEntityByHQL(hql2,
						admin.getEmail(), admin.getPwd());
				if (adminList2.size() == 0) {
					message = "*亲，密码不正确";
					return "wrong";
				} else {
					admin = adminList2.get(0);
					return SUCCESS;
				}
			}
		} else {
			return "wrong";
		}

	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Dept> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Dept> deptList) {
		this.deptList = deptList;
	}

	/**
	 * @return the score
	 */
	public String getScore() {
		return score;
	}

	/**
	 * @param score
	 *            the score to set
	 */
	public void setScore(String score) {
		this.score = score;
	}

	/**
	 * @return the reason
	 */
	public String getReason() {
		return reason;
	}

	/**
	 * @param reason
	 *            the reason to set
	 */
	public void setReason(String reason) {
		this.reason = reason;
	}

	/**
	 * @return the userList
	 */
	public List<User> getUserList() {
		return userList;
	}

	/**
	 * @param userList
	 *            the userList to set
	 */
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public String getRePwd() {
		return rePwd;
	}

	public void setRePwd(String rePwd) {
		this.rePwd = rePwd;
	}

	public Honer getHoner() {
		return honer;
	}

	public void setHoner(Honer honer) {
		this.honer = honer;
	}

	public Integer getOffset() {
		return offset;
	}

	public void setOffset(Integer offset) {
		this.offset = offset;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

}
